<!DOCTYPE html>
<html>
<head>

<!-- meta tags -->
<meta charset="utf-8">
<meta name="keywords" content="bootstrap 4, premium, multipurpose, ecommerce, html5, CSS" />
<meta name="description" content="Bootstrap 4 Landing Page Template" />
<meta name="author" content="www.themesground.com" />
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

<!-- Title -->
<title>佩奇牧场·购物车</title>
  <!-- Favicon Icon -->
  <link rel="shortcut icon" href="../assets/images/pagepig.ico" />
  <!-- inject css start -->
  <link href="../assets/css/theme-plugin.css" rel="stylesheet" />
  <link href="../assets/css/theme.min.css" rel="stylesheet" />

  <!-- inject css end -->

</head>
<body>
<!-- 引入head -->
<div class="head"></div>
<!-- page wrapper start -->
<div class="page-wrapper">
  <!-- preloader start -->
  <div id="ht-preloader">
    <div class="loader clear-loader"> <img class="img-fluid" src="../assets/images/loader.gif" alt=""> </div>
  </div>
  <!-- preloader end -->
<section class="bg-light py-4">
  <div class="container">
    <div class="row align-items-center">
      <div class="col-md-6">
        <h1 class="h2 mb-0">我的购物车</h1>
      </div>
      <div class="col-md-6 mt-3 mt-md-0">
        <nav aria-label="breadcrumb">
          <ol class="breadcrumb justify-content-md-end bg-transparent p-0 m-0">
            <li class="breadcrumb-item"><a class="link-title" href="http://127.0.0.1:8888/page/index.html">主页</a>
            </li>
            <!-- <li class="breadcrumb-item"><a class="link-title" href="#">Shop</a></li> -->
            <li class="breadcrumb-item active text-primary" aria-current="page">我的购物车</li>
          </ol>
        </nav>
      </div>
    </div>
    <!-- / .row -->
  </div>
  <!-- / .container -->
</section>



<!--body content start-->

<div class="page-content">

<section>
  <div class="container">
    <div class="row">
      <div class="col-lg-8">
        <div class="table-responsive">
          <table class="cart-table table">
            <thead>
              <tr>
                <th scope="col">商品</th>
                <th scope="col">单价</th>
                <th scope="col">数量</th>
                <th scope="col">金额</th>
              </tr>
            </thead>
            <tbody  id="itemcart">

<!--append追加数据-->
            </tbody>
          </table>
        </div>
        
      </div>
      <div class="col-lg-4 pl-lg-5 mt-8 mt-lg-0" id="itemcartall">

<!--all  append追加数据-->
      </div>
    </div>
   
  </div>
</section>

		</div>
	</div>
</div>



<!--back-to-top start-->

<div class="scroll-top"><a class="smoothscroll" href="#top"><i class="las la-angle-up"></i></a></div>

<!--back-to-top end-->
<!-- 引入footer -->
<div class="footer"></div>
<!-- inject js start -->

<script src="../assets/js/jquery-3.5.1.min.js"></script> <script src="../assets/js/popper.min.js"></script>
<script src="../assets/js/bootstrap.min.js"></script>
<!--<script src="../assets/js/owl.carousel.min.js"></script>-->
<script src="../assets/js/light-slider.js"></script>
<script src="../assets/js/parallax.js"></script>
<script src="../assets/js/magnific-popup.min.js"></script>
<script src="../assets/js/jquery.countdown.min.js"></script>
<script src="../assets/js/jquery.dd.min.js"></script>
<script src="../assets/js/validator.js"></script>
<script src="../assets/js/wow.js"></script>
<script src="../assets/js/theme-script.js"></script>
<!-- inject js end -->
<script>
	function showAll(){
      $("#itemcart").empty();
      $("#itemcartall").empty();
      $.ajax({
        url:"/page/cartShow",
        type:"post",
        contentType:"application/x-www-form-urlencoded; charset=utf-8",
        dataType:"json",
        /* data:{userId:1}, */
        success:function (data){
          /*console.log(data.data);展示所有数据*/
          $("#itemcart").empty();
          let itemList=data.data;
          let AllItemPrice=0;
          for(let i=0;i<itemList.length;i++){
            let itemPrice=itemList[i].itemPrice/100;
            let oneItemPrice=(itemList[i].itemPrice*itemList[i].num)/100;
            AllItemPrice+=oneItemPrice;
            let tr=`<tr>
                <td>
                  <div class="cart-thumb media align-items-center">
                    <a href="#">
                      <img class="img-fluid" src="${itemList[i].itemImage}" alt="">
                    </a>
                    <div class="media-body ml-3">
                      <div class="product-title mb-2"><a class="link-title" href="#">${itemList[i].itemTitle}</a>
                      </div>
                    </div>
                  </div>
                </td>
                <td> <span class="product-price text-muted">${itemPrice}</span>
                </td>
                <td>
                  <div class="d-flex align-items-center">
                    <button class="btn-product btn-product-up" value="${itemList[i].num}" name="${itemList[i].itemPrice}" cartId="${itemList[i].cartId}" onclick="delItemNum(this)"> <i class="las la-minus"></i></button>
                    <input  class="form-product" type="number" min="1" id="${itemList[i].itemId}" name="${itemList[i].itemId}" itemId="${itemList[i].itemId}" cartId="${itemList[i].cartId}" value="${itemList[i].num}" onchange="changeInputNum(this)">
                    <button class="btn-product btn-product-down" value="${itemList[i].num}" name="${itemList[i].itemPrice}" cartId="${itemList[i].cartId}" onclick="addItemNum(this)"> <i class="las la-plus"></i></button>
                  </div>
                </td>
                <td> <span id="${itemList[i].cartId} class="product-price text-dark font-w-6" >${oneItemPrice}</span>
                 <p class="close-link" ><i class="las la-times" name="${itemList[i].cartId}" onclick="delItemByCartId(this)"></i></p>
                </td>
              </tr>`;
            $("#itemcart").append(tr);
          }

          let all=`<div class="border rounded p-5 bg-light-4">
          <h4 class="text-black text-left mb-2 font-w-6">总计</h4>
          <div class="d-flex justify-content-between align-items-center border-bottom py-3"> <span class="text-muted">总金额</span>  <span class="text-dark">￥ ${AllItemPrice.toFixed(2)}</span>
          </div>
          <div class="d-flex justify-content-between align-items-center border-bottom py-3"> <span class="text-muted">优惠金额</span>  <span class="text-dark">-0.00</span>
          </div>
          <div class="d-flex justify-content-between align-items-center pt-3 mb-5"> <span class="text-dark h5">订单金额</span>  <span class="text-dark font-w-6 h5" name="allItemPrice">￥ ${AllItemPrice.toFixed(2)}</span>
          </div> <button class="btn btn-primary btn-animated btn-block" id="orderCheckOut" onclick="orderCheckOut()">去结算</button>
          <a class="btn btn-dark btn-animated mt-3 btn-block" href="http://127.0.0.1:8888/page/index.html">继续购物</a>
        </div>`
          $("#itemcartall").empty().append(all);
        }
      });
    }
	
	/*查询数据库，显示购物车列表*/
	$(function(){
		/*alert("测试开始");*/
      showAll();
	});

	/*点击提交订单*/
	function orderCheckOut(){
      /*$('.orderCheckOut').attr("disabled",true);*/
      window.location.href="order.html";
    }

	/*购物车页面点击X按钮删除一列*/
    function delItemByCartId(obj){
    let cartId=obj.getAttribute("name");
    var mymessage=confirm("您确认删除吗?");
    if(mymessage==true){
      $.ajax({
        url:"/page/cartDelItemCartById",
        type:"get",
        data:{cartId:cartId},
        contentType:"application/x-www-form-urlencoded; charset=utf-8",
        dataType:"json",
        success:function (data){
          showAll();
        },
        error:function () {
          alert("删除失败，请联系管理员！")
        }
      })

    }
  }

  /*手动输入数量，变更数量*/
  function changeInputNum(obj){
    let num =eval($(obj).val());
    let cartId=obj.getAttribute("cartId");
    if (num >= 1){
      $.ajax({
        url:"/cart/changNum",
        type:"get",
        data:{num:num,cartId:cartId},
        contentType:"application/x-www-form-urlencoded; charset=utf-8",
        dataType:"json",
        success:function (data){
          showAll()
        }
      })

    }else if(num <=0 ){
     alert("修改失败..商品数量不能小于1！");
      showAll()
    }
  }

	/*购物车减商品，更新数量，更新数据库*/
    function delItemNum(obj){
      let cartId=obj.getAttribute("cartId");
      let _thisInput = $(obj).siblings("input");
      let num=_thisInput.val();
      console.log("购物车商品的id="+cartId);
      if (num<=1){
        console.log("商品数量="+num+"不可减少");
        return true;
      }else {
        console.log("商品数量="+num+"修改后为"+(num-1));
        _thisInput.val(eval(_thisInput.val()) - 1);
        num=(eval(num) - 1);
        $.ajax({
          url:"/cart/changNum",
          type:"get",
          data:{num:num,cartId:cartId},
          contentType:"application/x-www-form-urlencoded; charset=utf-8",
          dataType:"json",
          success:function (data){
            showAll()
          }
        })

      }
    }

    /*购物车加商品，更新数量，更新数据库*/
    function addItemNum(obj){
      let cartId=obj.getAttribute("cartId");
      let _thisInput = $(obj).siblings("input");
      let num=_thisInput.val();
      console.log("购物车商品的id="+cartId);
      console.log("商品数量="+num+"修改后为"+(eval(num) + 1));
        _thisInput.val(eval(_thisInput.val()) + 1);
        $.ajax({
          url:"/cart/changNum",
          type:"get",
          data:{num:(eval(num) + 1),cartId:cartId},
          contentType:"application/x-www-form-urlencoded; charset=utf-8",
          dataType:"json",
          success:function (data){showAll();}
        })

    }
</script>
<script type="text/javascript">
  //在js中引入head与footer
  $(document).ready(function () {
    $('.head').load('head.html');
    $('.footer').load('footer.html');
  });
</script>
</body>
</html>
